package geometry;

import ru.ts.gisutils.geometry.IXY;
import ru.ts.gisutils.geometry.IXYBuf;
import ru.ts.gisutils.geometry.XYBuf;
import ru.ts.gisutils.geometry.XY;
import ru.ts.common.arrays.DirectPointArray;
import static ru.ts.common.misc.Text.serr;

import java.util.ListIterator;

/**
 * Created by IntelliJ IDEA.
 * User: sigolaev_va
 * Date: 25.01.2013
 * Time: 17:13:38
 * To change this template use File | Settings | File Templates.
 */
public class XYPolyline implements IXYPolyline
{
  private DirectPointArray m_buf;


  private class PolyIter implements ListIterator<IXYLine>
  {
    int m_pos;

    private PolyIter()
    {
      m_pos = 0;
    }

    public boolean hasNext()
    {
      return m_pos < ( m_buf.size() - 1 );
    }

    public IXYLine next()
    {
      IXYLine line = new XYLine( new XY( m_buf.getX( m_pos ), m_buf.getY( m_pos ) ), new XY( m_buf.getX( m_pos + 1 ), m_buf.getY( m_pos + 1 ) ) );
      m_pos++;
      return line;
    }

    public boolean hasPrevious()
    {
      return m_pos > 0;
    }

    public IXYLine previous()
    {
      m_pos--;
      return new XYLine( new XY( m_buf.getX( m_pos ), m_buf.getY( m_pos ) ), new XY( m_buf.getX( m_pos + 1 ), m_buf.getY( m_pos + 1 ) ) );
    }

    public int nextIndex()
    {
      return m_pos;
    }

    public int previousIndex()
    {
      return m_pos - 1;
    }

    public void remove()
    {
      m_buf.remove( m_pos );
    }

    public void set( IXYLine ixyLine )
    {
      m_buf.set2X( m_pos, ixyLine.startPoint().getX() );
      m_buf.set2Y( m_pos, ixyLine.startPoint().getY() );
      m_buf.set2X( m_pos, ixyLine.endPoint().getX() );
      m_buf.set2Y( m_pos, ixyLine.endPoint().getY() );
    }

    public void add( IXYLine ixyLine )
    {
      serr( "No action for add IXYLine" );
    }
  }

  public XYPolyline( DirectPointArray buf )
  {
    m_buf = buf;
  }

  public XYPolyline()
  {
    m_buf = new DirectPointArray();
  }

  public int size()
  {
    return m_buf.size();
  }

  public IXYLine line( int index ) throws ArrayIndexOutOfBoundsException
  {
    return new XYLine( new XY( m_buf.getX( index ), m_buf.getY( index ) ), new XY( m_buf.getX( index + 1 ), m_buf.getY( index + 1 ) ) );
  }

  public ListIterator<IXYLine> getIterator()
  {
    return new PolyIter();
  }

  public void split( int index, IXY pnt )
  {
    m_buf.insert(index + 1, pnt.getX(), pnt.getY());
  }

  public void insertBefore( int index, IXY pnt )
  {
    m_buf.insert(index, pnt.getX(), pnt.getY());
  }

  public void append( IXY point )
  {
    //To change body of implemented methods use File | Settings | File Templates.
  }
}
